import {isEmpty} from '/src/utils/types.js'
import {getCurrentInstance} from "vue"

export function useHookMixin() {

// 引入消息总线
  const cxt  = getCurrentInstance() //相当于Vue2中的this
  const bus = cxt.appContext.config.globalProperties.$bus

  /**
   * 判断是否为普通 整句 文本框
   * @param formType 字段类型
   */
  function isTrimInput(formType, disabled) {
    // floatnumber 类型disabled 为true，用input展示，防止掩码被置成0
    if (formType == 'floatnumber' && disabled) {
      return true
    }
    return [
      'mobile',
      'email',
      'website'
    ].includes(formType)
  }

  /**
   * 获取类型图标
   * @param {*} formType
   */
  function getInputIcon(formType) {
    return {
      mobile: 'wk wk-icon-mobile',
      email: 'wk ',
      website: 'wk wk-icon-link'
    }[formType]
  }

  /**
   * 获取输入最大长度
   * @param {*} formType
   */
  function getInputMaxlength(formType) {
    if (formType === 'website') {
      return 800
    }
    return 100
  }






  /**
   * 用于解决第一次点击时间组件，不能弹出的问题。手动弹出时间选择框
   * @param e
   */
  function dateTimeFocus(e) {
    console.log("focus", e)
    console.log("focus", cxt)
    // this.$refs.datetime.handleOpen()
    cxt.refs.datetime.handleOpen()
  }

  function panelChange(date, mode, view) {
    console.log("panelChange", date)
    console.log("panelChange", mode)
    console.log("panelChange", view)
  }

  function visibleChange(visibility) {
    console.log("visibleChange", visibility)
  }

  /**
   * 判断是空值
   */
  function isEmptyValue(value) {
    return value === null || value == undefined
  }



  /**
   * 选择省市区
   */
  function selectProvince(data, item, index) {
    this.fieldFrom[item.field].province = data.value
    this.addressChange(item, index, data, this.fieldFrom[item.field])
  }

  function selectCity(data, item, index) {
    this.fieldFrom[item.field].city = data.value
    this.addressChange(item, index, data, this.fieldFrom[item.field])
  }

  function selectArea(data, item, index) {
    this.fieldFrom[item.field].area = data.value
    this.addressChange(item, index, data, this.fieldFrom[item.field])
  }



  /**
   * 获取提示语
   */
  function getTips(data) {
    const tips = data.tips || data.inputTips
    if (data.tipType == 'tooltip') {
      return isEmpty(tips) ? '' : tips
    }
    return isEmpty(tips) ? '' : `（${tips}）`
  }

  /**
   * 判断展示
   */
  function getShowValue(item) {
    if (item.hasOwnProperty('show')) {
      return item.show
    }
    return true
  }

  return {
    isTrimInput, getInputIcon, getInputMaxlength,
     dateTimeFocus, panelChange, visibleChange,
    isEmptyValue,  selectProvince, selectCity,
    selectArea, getTips, getShowValue
  }
}